package com.aiyige.utils;

import android.content.Context;
import android.content.res.Resources;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by zhuyakun on 2017/10/31.
 */

public class StandardCoverLayoutBehavior extends CoordinatorLayout.Behavior<View> {
    boolean layouted = false;

    public StandardCoverLayoutBehavior() {
    }

    public StandardCoverLayoutBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
        if (!layouted) {
            int[] location = new int[2];
            parent.getLocationOnScreen(location);
            int screenHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
            CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
            layoutParams.topMargin = (screenHeight - location[1] - child.getMeasuredHeight()) / 2;
            layouted = true;
        }
        return false;
    }
}
